package cz.muni.fi.pa165.library.web_layer.adminPages.Borrow;

import cz.muni.fi.pa165.library.web_layer.adminPages.Borrow.AddBorrow;
import cz.muni.fi.pa165.library.api.BorrowService;
import cz.muni.fi.pa165.library.api.BorrowTO;
import cz.muni.fi.pa165.library.web_layer.ApplicationContextProvider;
import cz.muni.fi.pa165.library.web_layer.HomePage;
import cz.muni.fi.pa165.library.web_layer.Template;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import java.util.Locale;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.AbstractItem;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;

/**
 *
 * @author Filip Ligac
 */
public class ShowAllBorrow extends Template {
    private static final long serialVersionUID = 30L; 
    public ShowAllBorrow(final PageParameters parameters) {
	super(parameters);
        
        BorrowService borrowService = (BorrowService) ApplicationContextProvider.getApplicationContext().getBean("borrowService");
        
        // RETURN BORROW
        StringValue borrowId = parameters.get("borrowId");
        if (!borrowId.isEmpty()) {
            BorrowTO borrow = borrowService.findBorrowByID(borrowId.toLong());
            borrowService.returnBooks(borrow, borrow.getTitlesTO());
            setResponsePage(ShowAllBorrow.class);
        }
        
        // EXTEND BORROW
        StringValue extend = parameters.get("extend");
        if (!extend.isEmpty()) {
            BorrowTO borrow = borrowService.findBorrowByID(extend.toLong());
            
            Calendar cal = Calendar.getInstance();  
            cal.setTime(borrow.getExpirationDate());  
            cal.add(Calendar.MONTH, 1); // add 1 month  

            borrow.setExpirationDate(cal.getTime());             
            borrowService.updateExpirationDate(borrow, borrow.getExpirationDate());
            setResponsePage(ShowAllBorrow.class);
        }

        RepeatingView repeating = new RepeatingView("repeating");
        
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        
        for (BorrowTO borrow : borrowService.findAllBorrows()) {
            AbstractItem item = new AbstractItem(repeating.newChildId());
            PageParameters pageParameters = new PageParameters();
            pageParameters.add("borrowId", borrow.getBorrowID());
            PageParameters pageParameters2 = new PageParameters();
            pageParameters2.add("extend", borrow.getBorrowID());
            item.add(new BookmarkablePageLink<Void>("extend", ShowAllBorrow.class, pageParameters2));
            item.add(new BookmarkablePageLink<Void>("return", ShowAllBorrow.class, pageParameters));
            item.add(new Label("reader", borrow.getReaderTO().toString()));
            if (borrow.getTitlesTO().size() > 0) {
                item.add(new Label("title", borrow.getTitlesTO().toString()));      
            } else {
                item.add(new Label("title", new Model("[RETURNED]")));
            }
            item.add(new Label("borrowdate", dateFormat.format(borrow.getBorrowDate())));
            item.add(new Label("expirationdate", dateFormat.format(borrow.getExpirationDate())));
            repeating.add(item);
       }
        
       add(repeating);
       
       BookmarkablePageLink homeLink = new BookmarkablePageLink<Void>(
                "home", HomePage.class);
       add(homeLink);
       BookmarkablePageLink addLink = new BookmarkablePageLink<Void>(
                "addBorrow", AddBorrow.class);
       add(addLink);
    }
}